package cn.zsb.servlet;

import org.springframework.data.redis.core.RedisTemplate;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author oldliu
 * @since 1.0
 */
public class TimeFilter implements Filter {

    private RedisTemplate redisTemplate;

    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String host = ((HttpServletRequest) servletRequest).getRequestURI();
        System.out.println("???????????????????????"+host);
        if(((HttpServletRequest) servletRequest).getMethod().equals("OPTIONS"))
            return ;
        if (redisTemplate.opsForValue().get(host) == null)
            redisTemplate.opsForValue().set(host, 1);
        else
            redisTemplate.opsForValue().increment(host);
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
